//
//  CalculateVC.m
//  Water
//
//  Created by yh on 2018/1/15.
//  Copyright © 2018年 chenguibang. All rights reserved.
//

#import "CalculateVC.h"
#import "ExercisePickerView.h"
@interface CalculateVC ()

@end

@implementation CalculateVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.calculateViewModel addObserver:self forKeyPath:@"target" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)pickEx:(UIButton *)sender {
    ExercisePickerView *pickView = WaterXIB(@"ExercisePickerView");

    NSArray *level = @[
                       @{
                           @"title":@"无运动",
                           @"level":@(0),
                         },
                       
                       @{
                           @"title":@"少量运动",
                           @"level":@(1),
                           },
                       
                       @{
                           @"title":@"大量运动",
                           @"level":@(2),
                           },
                       
                       @{
                           @"title":@"剧烈运动",
                           @"level":@(3),
                           },
                       ];
    
    
    [pickView pickWithData:level titleBlock:^NSString *(NSInteger index, NSDictionary* dataItem) {
        return (NSString *)dataItem[@"title"];
    } resultBlock:^void(NSInteger index, id dataItem) {
        [sender setTitle:(NSString *)dataItem[@"title"] forState:UIControlStateNormal];
    } at:self];
}


- (IBAction)calculate:(id)sender {
    self.calculateViewModel.weight = @([self.weightTextFiled.text intValue]);
    [self.calculateViewModel calculate];
}


- (IBAction)setAsTodayTarget:(id)sender {
    [AppClient shared].todayTarget = self.calculateViewModel.target;
    [[AppClient shared] savePlans];
}


- (CalculateViewModel *)calculateViewModel{
    if (!_calculateViewModel) {
        _calculateViewModel = [[CalculateViewModel alloc]init];
    }
    return _calculateViewModel;
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"target"]) {
        self.recomanAmount.text = [NSString stringWithFormat:@"%@ml",self.calculateViewModel.target];
    }
}


- (void)dealloc{
    [self.calculateViewModel removeObserver:self forKeyPath:@"target"];
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
